/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.unicode.unihan.domain;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.unicode.unihan.parser.UnihanLineParser;

/**
 *
 * @author Andrew
 */
// TODO: should this really be an entity?
@Entity
@Table(name="unihan_row",
       uniqueConstraints=@UniqueConstraint(columnNames={"unicode", "unihan_key", "unihan_value"}))
public class UnihanRow implements Serializable {

    // TODO: remove these constants (they've been moved to the parser)
    public final static String UNICODE_PREFIX = "U+";
    public final static String KEY_PREFIX ="k";
    public final static String FIELD_SEPARATOR = "\t";

    private final static UnihanLineParser parser =
            new UnihanLineParser();

    // FIXME: change the type to Long
    @Id
    @GeneratedValue
    private Integer id;

    @Column(nullable=false)
    private Integer unicode;

    @Column(nullable=false, length=31, name="unihan_key")
    private String key;

    @Column(nullable=false, length=4096, name="unihan_value")
    private String value;

    public UnihanRow() {
    }

    public UnihanRow(Integer unicode, String code, String value) {
        this.unicode = unicode;
        this.key = code;
        this.value = value;
    }

    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * @return the unicode
     */
    public Integer getUnicode() {
        return unicode;
    }

    /**
     * @param unicode the unicode to set
     */
    public void setUnicode(Integer unicode) {
        this.unicode = unicode;
    }

    /**
     * @return the code
     */
    public String getKey() {
        return key;
    }

    /**
     * @param code the code to set
     */
    public void setKey(String code) {
        this.key = code;
    }

    /**
     * @return the value
     */
    public String getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Unicode: "+ Integer.toHexString(unicode) + ", key: " +
                key +  ", value: " + value;
    }

    @Override
    public int hashCode() {
        return unicode.hashCode() + key.hashCode() + value.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final UnihanRow other = (UnihanRow) obj;
        if (this.unicode != other.unicode && (this.unicode == null || !this.unicode.equals(other.unicode))) {
            return false;
        }
        if ((this.key == null) ? (other.key != null) : !this.key.equals(other.key)) {
            return false;
        }
        if ((this.value == null) ? (other.value != null) : !this.value.equals(other.value)) {
            return false;
        }
        return true;
    }

}
